package com.bjx.demo.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjx.api.flow.constant.TaskDisposeConst;
import com.bjx.api.flow.domain.vo.FlowDisposeVO;
import com.bjx.api.flow.domain.vo.FlowNodeVO;
import com.bjx.api.flow.domain.vo.FlowStartVO;
import com.bjx.api.flow.service.RemoteFlowService;
import com.bjx.api.flow.service.RemoteTaskDisposeService;
import com.bjx.common.core.constant.FrameConst;
import com.bjx.common.core.domain.R;
import com.bjx.common.core.exception.BJXException;
import com.bjx.common.security.util.LoginUtil;
import com.bjx.demo.domain.entity.VacationEntity;
import com.bjx.demo.domain.query.VacationQuery;
import com.bjx.demo.domain.vo.VacationVO;
import com.bjx.demo.mapper.VacationMapper;
import com.bjx.demo.service.IVacationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @desc: 请假信息服务实现
 * @author: liushuai
 * @date: 2022/10/20
 */
@Service
public class VacationServiceImpl extends ServiceImpl<VacationMapper, VacationEntity> implements IVacationService {
    @Autowired
    private VacationMapper vacationMapper;
    @Resource
    private RemoteFlowService remoteFlowService;

    @Resource
    private RemoteTaskDisposeService remoteTaskDisposeService;

    @Override
    public IPage<VacationVO> listVacationPage(VacationQuery vacationQuery) {
        IPage<VacationVO> iPage = vacationMapper.listVacationPage(vacationQuery.getPage(), vacationQuery);
        return iPage;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertVacation(VacationVO vacationVO) {
        VacationEntity entity = Convert.convert(VacationEntity.class, vacationVO);
        vacationMapper.insert(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateVacation(VacationVO vacationVO) {
        VacationEntity entity = Convert.convert(VacationEntity.class, vacationVO);
        vacationMapper.updateById(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteVacationByVacationId(Long vacationId) {
        vacationMapper.deleteById(vacationId);
    }

    @Override
    public VacationVO selectVacationByVacationId(Long vacationId) {
        VacationEntity entity = vacationMapper.selectById(vacationId);
        VacationVO vo = Convert.convert(VacationVO.class, entity);
        return vo;
    }

    @Override
    public void startVacationFlow(VacationVO vacationVO) {
        R<FlowNodeVO> r1 = remoteFlowService.selectFirstFlowNodeInfoByDefinitionKey("b8c03528-a1d4-4f3c-ba1d-1d67c64f0bec");
        if (r1.getCode() == R.FAIL) {
            throw new BJXException("获取第一个审批节点失败");
        }
        FlowNodeVO node = r1.getData();
        if (CollectionUtil.isEmpty(node.getUsers())) {
            throw new BJXException("审批节点用户不存在");
        }
        Map<String,Object> params = new HashMap<>();
        params.put(node.getId(),node.getUsers().stream().map(res->res.getUserId()).collect(Collectors.joining()));
        R<String> r = remoteFlowService.startFlow(FlowStartVO.builder()
                 .variables(params)
                .businessId(vacationVO.getVacationId())
                .definitionKey("b8c03528-a1d4-4f3c-ba1d-1d67c64f0bec")
                        .businessSign("AddOrUpdateVacation")
                        .name(LoginUtil.loginInfo().getRealName()+"发起的请假审批")
                .build());
        if (r.getCode() == FrameConst.FAIL) {
            throw new BJXException("流程启动失败");
        }
        String data = r.getData();

    }

    @Override
    public void vacationFlowDispose(FlowDisposeVO flowDisposeVO) {
        switch (flowDisposeVO.getApproveType()){
            case TaskDisposeConst.PASS:
                remoteTaskDisposeService.pass(flowDisposeVO);
                break;
            case TaskDisposeConst.REJECT:
                break;
        }
    }


}
